package com.tfswx.jcyw.ruleenginedesigner.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.tfswx.jcyw.ruleenginedesigner.dao.GzyqXtYhMapper;
import com.tfswx.jcyw.ruleenginedesigner.dao.model.GzyqXtYh;
import com.tfswx.jcyw.ruleenginedesigner.dto.yhgl.*;
import com.tfswx.jcyw.ruleenginedesigner.service.YhglService;
import com.tfswx.jcyw.ruleenginedesigner.util.JwtUtil;
import com.tfswx.jcyw.ruleenginedesigner.util.UserManger;
import com.tfswx.ruleengine.compute.exception.AppException;
import com.tfswx.ruleengine.compute.util.CommonUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author Ricky
 * @date 2021/5/11
 */
@Service
public class YhglServiceImpl implements YhglService {
    @Resource
    private GzyqXtYhMapper gzyqXtYhMapper;

    @Override
    public DlOutputDTO dl(DlInputDTO inputDTO) {
        GzyqXtYh gzyqXtYh = BeanUtil.copyProperties(inputDTO, GzyqXtYh.class);
        gzyqXtYh = gzyqXtYhMapper.getByDlbmAndKl(gzyqXtYh);
        if (gzyqXtYh == null) {
            throw new AppException("用户名或密码错误");
        }
        DlOutputDTO dlOutputDTO = new DlOutputDTO();
        dlOutputDTO.setYhxx(gzyqXtYh);
        String token = JwtUtil.generateToken(UserManger.toUserInfo(gzyqXtYh));
        dlOutputDTO.setToken(token);
        return dlOutputDTO;
    }

    @Override
    public String addYh(YhAddInputDTO inputDTO) {
        GzyqXtYh gzyqXtYh = BeanUtil.copyProperties(inputDTO, GzyqXtYh.class);
        gzyqXtYh.setRybm(CommonUtil.getUuid());
        gzyqXtYh.setSfsc("N");
        gzyqXtYh.setCjsj(new Date());
        gzyqXtYh.setZhxgsj(new Date());
        int insert = gzyqXtYhMapper.insert(gzyqXtYh);
        if (insert == 1) {
            return gzyqXtYh.getRybm();
        }
        throw new AppException("注册用户失败");

    }

    @Override
    public List<YhListQueryOutputDTO> queryYhList(YhListQueryInputDTO inputDTO) {
        GzyqXtYh gzyqXtYh = BeanUtil.copyProperties(inputDTO, GzyqXtYh.class);
        return gzyqXtYhMapper.queryYhList(gzyqXtYh);
    }

    @Override
    public boolean updateYh(YhUpdateInputDTO inputDTO) {
        GzyqXtYh gzyqXtYh = BeanUtil.copyProperties(inputDTO, GzyqXtYh.class);
        gzyqXtYh.setZhxgsj(new Date());
        int i = gzyqXtYhMapper.update(gzyqXtYh);
        if (i != 1) {
            throw new AppException("修改失败");
        }
        return true;
    }

    @Override
    public boolean undeleteYh(YhUndeleteInputDTO inputDTO) {
        GzyqXtYh gzyqXtYh = BeanUtil.copyProperties(inputDTO, GzyqXtYh.class);
        gzyqXtYh.setZhxgsj(new Date());
        int i = gzyqXtYhMapper.undelete(gzyqXtYh);
        if (i != 1) {
            throw new AppException("修改失败");
        }
        return true;
    }

    @Override
    public boolean deleteYh(YhDeleteInputDTO inputDTO) {
        GzyqXtYh gzyqXtYh = BeanUtil.copyProperties(inputDTO, GzyqXtYh.class);
        gzyqXtYh.setZhxgsj(new Date());
        int i = gzyqXtYhMapper.delete(gzyqXtYh);
        if (i != 1) {
            throw new AppException("删除失败");
        }
        return true;
    }

    @Override
    public boolean setPassword(PasswordSetInputDTO inputDTO) {
        GzyqXtYh gzyqXtYh = BeanUtil.copyProperties(inputDTO, GzyqXtYh.class);
        gzyqXtYh.setZhxgsj(new Date());
        int i = gzyqXtYhMapper.setPassword(gzyqXtYh);
        if (i != 1) {
            throw new AppException("设置密码失败");
        }
        return true;
    }
}
